package 常用api;

public class 中断线程_自定义标志位 {

    private static volatile boolean isStop = false;

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //没有被中断就一直执行
                    while(!isStop){
                        //休眠一秒钟,打印一下
                        //但是如果说线程处于休眠/阻塞状态，就不会被中断
                        Thread.sleep(10000);
                        System.out.println("t run");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
        //让t线程运行三秒之后，再中断
        Thread.sleep(3000);
        isStop = true;
    }
}
